//
//  GMVideoCardCell.m
//  Teecloud
//
//  Created by xhkj on 2018/11/20.
//  Copyright © 2018 小志. All rights reserved.
//

#import "GMVideoCardCell.h"
#import "GMResultCell.h"
#import "GMExamModel.h"
#import "GMAnswerCards.h"

@interface GMVideoCardCell()<XMGWaterflowLayoutDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
/**标题*/
@property (weak, nonatomic) IBOutlet UILabel *titleName;
/**正确率*/
@property (weak, nonatomic) IBOutlet UILabel *correctRate;
/**总共用时*/
@property (weak, nonatomic) IBOutlet UILabel *totalTime;
/**平均用时*/
@property (weak, nonatomic) IBOutlet UILabel *averageTime;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation GMVideoCardCell

- (void)awakeFromNib {
    [super awakeFromNib];
    XMGWaterflowLayout *layout = [[XMGWaterflowLayout alloc] init];
    layout.delegate = self;
    self.collectionView.collectionViewLayout =layout;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"GMResultCell" bundle:nil] forCellWithReuseIdentifier:@"GMResultCell"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(noti:) name:@"postData" object:nil];
}
-(void)setModel:(GMExamModel *)model{
    _model = model;
    self.correctRate.text = [NSString stringWithFormat:@"%@",model.CorrectRate];
    self.totalTime.text = [NSString stringWithFormat:@"%ld",model.UseTime];
    self.averageTime.text = model.AverageTime;
    [self.dataArr removeAllObjects];
    [self.dataArr addObjectsFromArray:model.AnswerCards];
    [self.collectionView reloadData];
}
-(void)noti:(NSNotification *)noti{
    GMExamModel *model = [GMExamModel mj_objectWithKeyValues:[noti object]];
    self.correctRate.text = [NSString stringWithFormat:@"%@",model.CorrectRate];
    self.totalTime.text = [NSString stringWithFormat:@"%ld",model.UseTime];
    self.averageTime.text = model.AverageTime;
    [self.dataArr removeAllObjects];
    [self.dataArr addObjectsFromArray:model.AnswerCards];
    [self.collectionView reloadData];
}
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}
//每个section的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    GMResultCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GMResultCell" forIndexPath:indexPath];
    GMAnswerCards *model = self.dataArr[indexPath.row];
    model.tepName = @"随堂练习";
    cell.model = model;
    return cell;
}
#pragma mark - <XMGWaterflowLayoutDelegate>
- (CGFloat)waterflowLayout:(XMGWaterflowLayout *)waterflowLayout heightForItemAtIndex:(NSUInteger)index itemWidth:(CGFloat)itemWidth{
    //计算item的高度
    return itemWidth;
}
/**行间距*/
- (CGFloat)rowMarginInWaterflowLayout:(XMGWaterflowLayout *)waterflowLayout{
    return 10;
}
/**列间距*/
-(CGFloat)columnMarginInWaterflowLayout:(XMGWaterflowLayout *)waterflowLayout{
    return 10;
}
/**返回每行的个数*/
- (CGFloat)columnCountInWaterflowLayout:(XMGWaterflowLayout *)waterflowLayout{
    return 5;
}
- (UIEdgeInsets)edgeInsetsInWaterflowLayout:(XMGWaterflowLayout *)waterflowLayout{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
